/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package reportcenter.report.support.exporter;

import ar.com.fdvs.dj.core.DJConstants;
import ar.com.fdvs.dj.core.DynamicJasperHelper;
import ar.com.fdvs.dj.core.layout.ClassicLayoutManager;
import ar.com.fdvs.dj.domain.DynamicReport;
import ar.com.fdvs.dj.domain.ImageBanner;
import ar.com.fdvs.dj.domain.Style;
import ar.com.fdvs.dj.domain.builders.ColumnBuilder;
import ar.com.fdvs.dj.domain.builders.DynamicReportBuilder;
import ar.com.fdvs.dj.domain.constants.Border;
import ar.com.fdvs.dj.domain.constants.Font;
import ar.com.fdvs.dj.domain.constants.HorizontalAlign;
import ar.com.fdvs.dj.domain.constants.Transparency;
import ar.com.fdvs.dj.domain.constants.VerticalAlign;
import ar.com.fdvs.dj.domain.entities.columns.AbstractColumn;
import java.awt.Color;
import java.io.OutputStream;
import java.sql.Connection;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.fill.JRFileVirtualizer;
import org.openide.util.Exceptions;
import reportcenter.report.dominio.column.ReportColum;
import reportcenter.report.support.ReportManager;
import reportcenter.report.support.query.QueryInterceptor;
import reportcenter.support.dbutil.ConnectionManager;
import reportcenter.support.resourceload.DataSourceConfig;

/**
 *
 * @author renan
 */
public class PdfExporter implements ReportExporter {

    public boolean export(ReportManager rM) {
        
        Connection conn = null;
        try {
            new ConnectionManager(new DataSourceConfig(rM.REPORT.getIdD()));
            conn = ConnectionManager.ds.getConnection();

            //Definir ESTILO

            Style headerStyle = new Style();
            headerStyle.setFont(Font.VERDANA_MEDIUM_BOLD);
            headerStyle.setBorderBottom(Border.PEN_2_POINT);
            headerStyle.setHorizontalAlign(HorizontalAlign.CENTER);
            headerStyle.setVerticalAlign(VerticalAlign.MIDDLE);
            headerStyle.setBackgroundColor(Color.DARK_GRAY);
            headerStyle.setTextColor(Color.WHITE);
            headerStyle.setTransparency(Transparency.OPAQUE);
            headerStyle.setStretchWithOverflow(Boolean.TRUE);

            Style detailStyle = new Style();
            detailStyle.setStretchWithOverflow(Boolean.TRUE);


            Style titleStyle = new Style();
            titleStyle.setFont(new Font(16, Font._FONT_VERDANA, true));
            titleStyle.setStretchWithOverflow(Boolean.TRUE);
            titleStyle.setBackgroundColor(Color.GREEN);

            Style oddRowStyle = new Style();
            oddRowStyle.setBorder(Border.NO_BORDER);
            oddRowStyle.setBackgroundColor(Color.LIGHT_GRAY);
            oddRowStyle.setTransparency(Transparency.OPAQUE);


//            List<AbstractColumn> cols = new ArrayList<AbstractColumn>();
            DynamicReportBuilder drb = new DynamicReportBuilder();
            Integer margin = new Integer(10);
            drb.addSubtitle(rM.REPORT.getSubtitle()).addTitleStyle(titleStyle) //defines the style of the title
                    .addDetailHeight(new Integer(15)) //defines the height for each record of the report
                    .addLeftMargin(margin) //define the margin space for each side (top, bottom, right and left)
                    .addRightMargin(margin).addTopMargin(margin).addBottomMargin(margin).addColumnsPerPage(new Integer(1)).setPrintBackgroundOnOddRows(true).setOddRowBackgroundStyle(oddRowStyle).addFirstPageImageBanner("images/qq_medio.jpg", new Integer(122), new Integer(47), ImageBanner.ALIGN_RIGHT).addImageBanner("images/qq_medio.jpg", new Integer(122), new Integer(47), ImageBanner.ALIGN_RIGHT);

            for (ReportColum f : rM.REPORT.getReportColumCollection()) {
                AbstractColumn column = ColumnBuilder.getInstance() //creates a new instance of a ColumnBuilder
                        .addColumnProperty(f.getColumnName(), f.getClazz()) //defines the field of the data source that this column will show, also its type
                        .addTitle(f.getColumnDescr()) //the title for the column
                        .addWidth(f.getWidth()) //the width of the column
                        .addStyle(detailStyle) //defines the style for the records of this column
                        .addHeaderStyle(headerStyle) //defines the style for the title of this column
                        .build();

                drb.addColumn(column);
//                cols.add(column);

            }
            
            drb.setUseFullPageWidth(true);
            drb.setTitle(rM.REPORT.getTitle());
            
            drb.setQuery(new QueryInterceptor().transformToJasperParameters(rM.REPORT.getQuery(),rM.QUERY_PARAMS), DJConstants.QUERY_LANGUAGE_SQL);
            JasperPrint jp = null;
            DynamicReport dr = drb.build();
            
            
            for(Object o : rM.QUERY_PARAMS.keySet()){
                String chave = (String) o;
                System.out.println("Chave -> " + chave + " VALOR -> " + rM.QUERY_PARAMS.get(o));
            }
            
            
            
            jp = DynamicJasperHelper.generateJasperPrint(dr, new ClassicLayoutManager(), conn, rM.QUERY_PARAMS);

            JasperExportManager.exportReportToPdfFile(jp, rM.TARGET_FILE_PATH + System.getProperty("file.separator") + rM.TARGET_FILE_NAME);
        } catch (Exception ex) {
            Exceptions.printStackTrace(ex);
        } finally {
            try {
                conn.close();
            } catch (Exception ex) {
                Exceptions.printStackTrace(ex);
            }
        }
        return true;
    }
}
